package com.find.answer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.find.answer.domain.common.CustomException;
import com.find.answer.domain.po.Category;
import com.find.answer.domain.po.Dish;
import com.find.answer.domain.po.Setmeal;
import com.find.answer.mapper.CategoryMapper;
import com.find.answer.mapper.DishMapper;
import com.find.answer.mapper.SetmealMapper;
import com.find.answer.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Seon
 * @date 2022/10/19 11:02
 */
@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;
    @Override
    public boolean remove(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId, id);
        Integer dishCount = dishMapper.selectCount(dishWrapper);

        if (dishCount > 0) {
            throw new CustomException("该分类下已经关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId, id);
        Integer setmealCount = setmealMapper.selectCount(setmealWrapper);
        if (setmealCount > 0) {
            throw new CustomException("该分类下已经关联了套餐，不能删除");
        }
        super.removeById(id);
        return true;
    }
}
